package cn.mybatisplus;

import cn.mybatisplus.entity.Product;
import cn.mybatisplus.entity.User;
import cn.mybatisplus.mapper.ProductMapper;
import cn.mybatisplus.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MybatisPlusPaginTest_ {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private ProductMapper productMapper;

    @Test
    public void testPage(){
        Page<User> page = new Page<>(1,3);
        userMapper.selectPage(page,null);

        System.out.println(page);
        System.out.println(page.getRecords());
        System.out.println(page.getPages());
        System.out.println(page.getTotal());
        System.out.println(page.hasNext());
        System.out.println(page.hasPrevious());
    }


    @Test
    public void testSelectVo(){
        Page<User> page = new Page<>(1,3);
        userMapper.selectPageVo(page,20);

        System.out.println(page);
        System.out.println(page.getRecords());
        System.out.println(page.getPages());
        System.out.println(page.getTotal());
        System.out.println(page.hasNext());
        System.out.println(page.hasPrevious());
    }

    @Test
    public void testProduct01(){

        //小李查询商品价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李查询的价格 ：" + productLi.getPrice());
        //小王查询商品价格
        Product productW = productMapper.selectById(1);
        System.out.println("小王查询的价格 ：" + productW.getPrice());

        //小李将价格+50
        productLi.setPrice(productLi.getPrice() + 50);
        productMapper.updateById(productLi);
        //小王将价格+50
        productW.setPrice(productW.getPrice() - 30);
        int result = productMapper.updateById(productW);
        if (result == 0){
            //操作失败,重试
            Product productNew = productMapper.selectById(1);
            productNew.setPrice(productNew.getPrice() - 30);
            productMapper.updateById(productNew);
        }


        //老板查询商品价格
        Product productBoss = productMapper.selectById(1);
        System.out.println("老板查询的价格 ：" + productBoss.getPrice());

    }
}
